-- Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[p_WM_DonViTinh_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_DonViTinh_Insert]

IF OBJECT_ID(N'[dbo].[p_WM_DonViTinh_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_DonViTinh_Update]

IF OBJECT_ID(N'[dbo].[p_WM_DonViTinh_InsertUpdate]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_DonViTinh_InsertUpdate]

IF OBJECT_ID(N'[dbo].[p_WM_DonViTinh_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_DonViTinh_Delete]

IF OBJECT_ID(N'[dbo].[p_WM_DonViTinh_DeleteDynamic]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_DonViTinh_DeleteDynamic]

IF OBJECT_ID(N'[dbo].[p_WM_DonViTinh_Load]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_DonViTinh_Load]

IF OBJECT_ID(N'[dbo].[p_WM_DonViTinh_SelectDynamic]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_DonViTinh_SelectDynamic]

IF OBJECT_ID(N'[dbo].[p_WM_DonViTinh_SelectAll]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_WM_DonViTinh_SelectAll]


GO


------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_DonViTinh_Insert]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_DonViTinh_Insert]
	@Ma char(5),
	@Ten nvarchar(50)
AS
INSERT INTO [dbo].[t_WM_DonViTinh]
(
	[Ma],
	[Ten]
)
VALUES
(
	@Ma,
	@Ten
)

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_DonViTinh_Update]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_DonViTinh_Update]
	@Ma char(5),
	@Ten nvarchar(50)
AS

UPDATE
	[dbo].[t_WM_DonViTinh]
SET
	[Ten] = @Ten
WHERE
	[Ma] = @Ma

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_DonViTinh_InsertUpdate]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_DonViTinh_InsertUpdate]
	@Ma char(5),
	@Ten nvarchar(50)
AS
IF EXISTS(SELECT [Ma] FROM [dbo].[t_WM_DonViTinh] WHERE [Ma] = @Ma)
	BEGIN
		UPDATE
			[dbo].[t_WM_DonViTinh] 
		SET
			[Ten] = @Ten
		WHERE
			[Ma] = @Ma
	END
ELSE
	BEGIN
	INSERT INTO [dbo].[t_WM_DonViTinh]
	(
			[Ma],
			[Ten]
	)
	VALUES
	(
			@Ma,
			@Ten
	)	
	END
GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_DonViTinh_Delete]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_DonViTinh_Delete]
	@Ma char(5)
AS

DELETE FROM 
	[dbo].[t_WM_DonViTinh]
WHERE
	[Ma] = @Ma

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_DonViTinh_DeleteDynamic]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_DonViTinh_DeleteDynamic]
	@WhereCondition NVARCHAR(500)
AS

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 'DELETE FROM [dbo].[t_WM_DonViTinh] WHERE ' + @WhereCondition

EXEC sp_executesql @SQL

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_DonViTinh_Load]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_DonViTinh_Load]
	@Ma char(5)
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[Ma],
	[Ten]
FROM
	[dbo].[t_WM_DonViTinh]
WHERE
	[Ma] = @Ma
GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_DonViTinh_SelectDynamic]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_DonViTinh_SelectDynamic]
	@WhereCondition NVARCHAR(500),
	@OrderByExpression NVARCHAR(250) = NULL
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 
'SELECT 
	[Ma],
	[Ten]
FROM [dbo].[t_WM_DonViTinh] 
WHERE ' + @WhereCondition

IF @OrderByExpression IS NOT NULL AND LEN(@OrderByExpression) > 0
BEGIN
	SET @SQL = @SQL + ' ORDER BY ' + @OrderByExpression
END

EXEC sp_executesql @SQL

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_WM_DonViTinh_SelectAll]
-- Database: Warehouse_Management
-- Author: Ngo Thanh Tung
-- Time created: Tuesday, October 09, 2012
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_WM_DonViTinh_SelectAll]
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[Ma],
	[Ten]
FROM
	[dbo].[t_WM_DonViTinh]	

GO

